/**
 * @export
 * @param times 输入需要转化的时间参数
 * @param format 需要转化的格式
 * @returns 返回处理后的时间格式
 */
export function dateFormat (times, format) {
	let time = Date.parse(times);
	const between = (Date.now() - Number(time)) / 1000;
	if (between < 3600 && ((between / 60) < 1)) {
		return '刚刚'
	} else if (between < 3600) {
		return pluralize(~~(between / 60), '分钟')
	} else if (between < 86400) {
		return pluralize(~~(between / 3600), '小时')
	}
	if (!time) return null;
	let date = new Date(time);
	let o = {
		'M+': date.getMonth() + 1, // month
		'd+': date.getDate(), // day
		'h+': date.getHours(), // hour
		'm+': date.getMinutes(), // minute
		's+': date.getSeconds(), // second
		'q+': Math.floor((date.getMonth() + 3) / 3), // quarter
		'S': date.getMilliseconds() // millisecond
	};
	if (/(y+)/.test(format)) {
		format = format.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
	}
	for (let k in o) {
		if (new RegExp('(' + k + ')').test(format)) {
			format = format.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length))
		}
	}
	return format
}


function pluralize (time, label) {
	return time + label + '以前'
}

/**
 * @return {string}
 */
Date.prototype.Format = function (formatStr) {
	let str = formatStr;
	const Week = ['日', '一', '二', '三', '四', '五', '六'];
	str = str.replace(/yyyy|YYYY/, this.getFullYear());
	str = str.replace(/yy|YY/, (this.getYear() % 100) > 9 ? (this.getYear() % 100).toString() : '0' + (this.getYear() % 100));
	str = str.replace(/MM/, this.getMonth() > 8 ? (this.getMonth() + 1).toString() : '0' + (this.getMonth() + 1));
	str = str.replace(/M/g, this.getMonth() + 1);
	str = str.replace(/w|W/g, Week[this.getDay()]);
	str = str.replace(/dd|DD/, this.getDate() > 9 ? this.getDate().toString() : '0' + this.getDate());
	str = str.replace(/d|D/g, this.getDate());
	str = str.replace(/hh|HH/, this.getHours() > 9 ? this.getHours().toString() : '0' + this.getHours());
	str = str.replace(/h|H/g, this.getHours());
	str = str.replace(/mm/, this.getMinutes() > 9 ? this.getMinutes().toString() : '0' + this.getMinutes());
	str = str.replace(/m/g, this.getMinutes());
	str = str.replace(/ss|SS/, this.getSeconds() > 9 ? this.getSeconds().toString() : '0' + this.getSeconds());
	str = str.replace(/s|S/g, this.getSeconds());
	return str;
}

function getCurrentDate() {
	const date = new Date();

	let year = date.getFullYear();
	let month = date.getMonth() + 1;
	let day = date.getDate();

	month = month > 9 ? month : '0' + month;
	day = day > 9 ? day : '0' + day;

	return `${year}-${month}-${day}`;
}

function formatTime(time) {
	if (typeof time !== 'number' || time < 0) {
		return time
	}

	let hour = parseInt(time / 3600)
	time = time % 3600
	let minute = parseInt(time / 60)
	time = time % 60
	let second = time

	return ([hour, minute, second]).map(function (n) {
		n = n.toString()
		return n[1] ? n : '0' + n
	}).join(':')
}

function formatLocation(longitude, latitude) {
	if (typeof longitude === 'string' && typeof latitude === 'string') {
		longitude = parseFloat(longitude)
		latitude = parseFloat(latitude)
	}

	longitude = longitude.toFixed(2)
	latitude = latitude.toFixed(2)

	return {
		longitude: longitude.toString().split('.'),
		latitude: latitude.toString().split('.')
	}
}


function getByteLen(val) {
	var len = 0;
	for (var i = 0; i < val.length; i++) {
		var a = val.charAt(i);
		if (a.match(/[^\x00-\xff]/ig) != null) {
			len += 2;
		} else {
			len += 1;
		}
	}
	return len;
}


function isEmptyObject(e) {
	let t
	for (t in e)
		return !1
	return !0
}


module.exports = {
	isEmptyObject: isEmptyObject,
	getCurrentDate: getCurrentDate,
	formatTime: formatTime,
	formatLocation: formatLocation,
	dateFormat: dateFormat,
	getByteLen: getByteLen
}
